package top.deepdesigner.service.Impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;
import top.deepdesigner.pojo.DtsIssue;
import top.deepdesigner.service.DtsIssueService;

import java.util.Date;
import java.util.List;

/**
 * Start up <br/>
 * Created with IntelliJ IDEA. <br/>
 * Project Name: shop_manage <br/>
 * Author: duanruiqing <br/>
 * DateTime: 2022/9/24 星期六 15:01:56 <br/>
 */
@Service
public class DtsIssueServiceImpl extends ServiceImpl<BaseMapper<DtsIssue>, DtsIssue> implements DtsIssueService {
  @Override
  public Long getIssueCount() {
    return this.count(new LambdaQueryWrapper<DtsIssue>().eq(DtsIssue::getDeleted, false));
  }

  @Override
  public List<DtsIssue> getIssueList(Integer page, Integer limit, String sort, String order, String question) {
    QueryWrapper<DtsIssue> wrapper = new QueryWrapper<>();
    wrapper.eq(true, "deleted", false)
        .like(!StringUtils.isEmpty(question), "question", question)
        .orderBy(!StringUtils.isEmpty(sort), "asc".equalsIgnoreCase(order), sort);
    return this.page(new Page<>(page, limit), wrapper).getRecords();
  }

  @Override
  @Transactional
  public DtsIssue createIssue(DtsIssue issue) {
    List<DtsIssue> list = this.list(new LambdaQueryWrapper<DtsIssue>().eq(DtsIssue::getDeleted, false).eq(DtsIssue::getId, issue.getId()));
    if (list.size() > 0) {
      return null;
    }
    issue.setAddTime(new Date());
    issue.setUpdateTime(new Date());
    issue.setDeleted(false);
    this.save(issue);
    return issue;
  }

  @Override
  @Transactional
  public DtsIssue updateIssue(DtsIssue issue) {
    issue.setUpdateTime(new Date());
    LambdaQueryWrapper<DtsIssue> wrapper = new LambdaQueryWrapper<>();
    wrapper.eq(DtsIssue::getId, issue.getId());
    this.update(issue, wrapper);
    return issue;
  }

  @Override
  @Transactional
  public void deleteIssue(DtsIssue issue) {
    issue.setDeleted(true);
    LambdaQueryWrapper<DtsIssue> wrapper = new LambdaQueryWrapper<>();
    wrapper.eq(DtsIssue::getId, issue.getId());
    this.update(issue, wrapper);
  }
}
